package org.blogwu.learn.multithread.chapter3.demo3;

import java.util.concurrent.locks.ReentrantLock;

// 公平锁
public class Main {
    public static void main(String[] args) {
        FairLock fairLock = new FairLock("f1");
        FairLock fairLock2 = new FairLock("f2");
        fairLock.start();
        fairLock2.start();
    }
}

class FairLock extends Thread {
    public FairLock(String name) {
        super(name);
    }

    // 通过构造方法实现公平锁
    static ReentrantLock reentrantLock = new ReentrantLock(false);

    @Override
    public void run() {
        while (true) {
            try {
                reentrantLock.lock();
                System.out.println(Thread.currentThread().getName() + " 过去到锁");
            } finally {
                reentrantLock.unlock();
            }
        }
    }
}
